package com.finesys.backend.common.model.base;

import com.finesys.backend.common.constant.BusinessCode;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

/**
 * <p>Title: 业务返回信息</p>
 * <p>Description: </p>
 * <p>Copyright: CopyRight (c) 2020-2035</p>
 * <p>Company: finesys Co. LTD.</p>
 * <p>Author: lehoon</p>
 * <p>Date: 2020/10/22 11:03</p>
 */
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class BusinessResult<T> implements Serializable {
    /**
     * 业务操作码  约定2000成功 其他失败
     */
    private int code;

    /**
     * 返回信息
     */
    private String message;

    /**
     * 业务数据
     */
    private T data;

    public BusinessResult(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public BusinessResult<T> success(T message) {
        this.code = BusinessCode.SUCCESS.getCode();
        this.message = BusinessCode.SUCCESS.getMessage();
        this.data = message;
        return this;
    }

    public BusinessResult<T> fail() {
        this.code = BusinessCode.SYSTEM_EXCEPTION.getCode();
        this.message = BusinessCode.SYSTEM_EXCEPTION.getMessage();
        return this;
    }

    public BusinessResult<T> fail(T data, String message) {
        this.code = BusinessCode.SYSTEM_EXCEPTION.getCode();
        this.message = message;
        this.data = data;
        return this;
    }

    public BusinessResult<T> fail(int code, String message) {
        this.code = code;
        this.message = message;
        return this;
    }

    public BusinessResult<T> unavailable() {
        this.code = BusinessCode.SERVICE_UNAVAILABLE.getCode();
        this.message = BusinessCode.SERVICE_UNAVAILABLE.getMessage();
        return this;
    }

    public boolean isSuccess() {
        return this.code == BusinessCode.SUCCESS.getCode();
    }
}
